
import time
import datetime
from dateutil import parser
months = ('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

from lxml import etree
htmlparser = etree.HTMLParser()

Z = parser.parse('2007-01-01 00:00:00 GMT')
utctz = Z.tzinfo


def posix_to_int(date):
    seq = (int(date[12:16]), months.index(date[8:11]), int(date[5:7]),
           int(date[17:19]), int(date[20:22]), int(date[23:25]), 0, 1, -1)
    return int(time.mktime(time.struct_time(seq)))

def yyyymmdd_to_int(date):
    seq = (int(date[:4]), int(date[4:6]), int(date[6:8]), int(date[8:10]), int(date[10:12]), int(date[12:14]), 0, 1, -1)
    return int(time.mktime(time.struct_time(seq)))

def iso_to_int(date):
    seq = (int(date[:4]), int(date[5:7]), int(date[8:10]), int(date[11:13]), int(date[14:16]), int(date[17:19]), 0, 1, -1)
    return int(time.mktime(time.struct_time(seq)))

def int_to_dt(d):
    if type(d) != float:
        d = float(d)
    return datetime.datetime.fromtimestamp(d, utctz)

def dt_to_int(dt):
    return int(time.mktime(dt.timetuple()))
